package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageResult getPage(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
        Page<DishVO> page = dishMapper.getPage(queryDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.updateDish(dish);

        long id = dish.getId();
        long[] ids = {id};
        //删除原来的flavor
        dishMapper.deleteFlavorByDishIds(ids);
        //判断是否有flavor，没有则return
        if (dishDTO.getFlavors() == null || dishDTO.getFlavors().isEmpty()) {
            return;
        }

        //给flavor的dishId赋值
        dishDTO.getFlavors().forEach(flavor -> flavor.setDishId(dish.getId()));
        dishMapper.insertDishFlavor(dishDTO.getFlavors());
    }

    @Override
    public void updateStatus(DishDTO dishDTO) {
        Dish dish = new Dish();
        dish.setId(dishDTO.getId());
        dish.setStatus(dishDTO.getStatus());
        dishMapper.updateDish(dish);
    }

    @Override
    public DishVO getDishById(Long id) {
        //分别根据id查找到dish + dish_flavor + category(name字段) 的信息
        Dish dish = dishMapper.getDish(id);
        List<DishFlavor> dishFlavorList = dishMapper.getFlavorByDishId(id);
        String categoryName = dishMapper.getCategoryNameById(dish.getCategoryId());
        //赋值给DishVO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        dishVO.setCategoryName(categoryName);
        return dishVO;
    }

    @Transactional
    @Override
    public void delete(long[] arr) {
        for (long id : arr) {
            Dish dish = dishMapper.getDish(id);
            //如果是启售状态，抛出异常
            if (dish.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //如果菜品被套餐引用，抛出异常
            if (!dishMapper.getSetmealDishByDishId((long)id).isEmpty()) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //删除dish和关联的flavor和关联的setmeal_dish
//            dishMapper.deleteDish(id);
//            dishMapper.deleteFlavorByDishId((long)id);


        }
        //为了提高性能，使用动态sql删除dish和关联的setmeal_dish
        dishMapper.deleteDishByIds(arr);
        dishMapper.deleteFlavorByDishIds(arr);
    }

    @Override
    public void insertDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insertDish(dish);
        dishDTO.getFlavors().forEach(flavor -> flavor.setDishId(dish.getId()));
        dishMapper.insertDishFlavor(dishDTO.getFlavors());
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        //根据分类id查询起售状态的菜品
        List<Dish> dishes = dishMapper.getDishByCategoryId(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        for(Dish d : dishes) {
            //根据菜品id查询flavor
            List<DishFlavor> flavors = dishMapper.getFlavorByDishId(d.getId());
            DishVO dishvo = new DishVO();
            dishvo.setFlavors(flavors);
            BeanUtils.copyProperties(d,dishvo);
            dishVOList.add(dishvo);
        }
        return dishVOList;


    }


}
